package com.fage.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * @author lin
 * @version 1.0
 * @date 2020-08-10 17:16
 * @Description TODO
 */
@Configuration
@PropertySource(value = {"classpath:db/jdbc.properties"})
public class DataSourceConfigs {
    @Value("${url1}")
    String url1;
    @Value("${name1}")
    String name1;
    @Value("${password1}")
    String password1;
    @Value(("${driver1}"))
    String driver1;

    @Bean(value = "dataSource1")
    public DataSource createDataSource1() {
        DriverManagerDataSource source = new DriverManagerDataSource(url1, name1, password1);
        source.setDriverClassName(driver1);
        return source;
    }


    @Value("${driver2}")
    String url2;
    @Value("${driver2}")
    String name2;
    @Value("${driver2}")
    String password2;
    @Value(("${driver2}"))
    String driver2;

    @Bean(value = "dataSource2")
    public DataSource createDataSource2() {
        DriverManagerDataSource source = new DriverManagerDataSource(url2, name2, password2);
        source.setDriverClassName(driver2);
        return source;
    }

    @Bean(value = "jdbcTemplate")
    public JdbcTemplate createJdbcTemplate() {
        return new JdbcTemplate(createDataSource2());
    }

}
